/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.risk.setting.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.risk.setting.entity.RiskConfigLEntity;
import com.hopes.bizz.risk.setting.dto.RiskConfigLExcelDTO;
import com.hopes.bizz.risk.setting.vo.RiskConfigLExcelVO;
import com.hopes.bizz.risk.setting.mapper.RiskConfigLMapper;
import com.hopes.bizz.risk.setting.service.RiskConfigLService;
import com.hopes.boss.common.core.exception.BizException;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.entity.ValidateImportBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
/**
 * 事故发生的可能性设置
 *
 * @author pig
 * @date 2023-07-17 19:40:24
 */
@Service
public class RiskConfigLServiceImpl extends ServiceImpl<RiskConfigLMapper, RiskConfigLEntity> implements RiskConfigLService {

    @Override
    public boolean save(RiskConfigLEntity riskConfigL) {
        if (null == riskConfigL
                || StrUtil.isEmpty(riskConfigL.getModuleCode()) || null == riskConfigL.getRiskValue()) {
            throw new BizException("参数错误");
        }
        // 根据 riskValue 查询是否存在
        final LambdaQueryWrapper<RiskConfigLEntity> queryWrapper = Wrappers.<RiskConfigLEntity>lambdaQuery()
                .eq(RiskConfigLEntity::getModuleCode, riskConfigL.getModuleCode()).eq(RiskConfigLEntity::getRiskValue, riskConfigL.getRiskValue());
        final List<RiskConfigLEntity> riskConfigLEntityList = baseMapper.selectList(queryWrapper);
        if (CollUtil.isNotEmpty(riskConfigLEntityList)) {
            throw new BizException("值/等级因存在！");
        }
        return baseMapper.insert(riskConfigL) > 0;
    }

    @Override
    public boolean updateById(RiskConfigLEntity riskConfigL) {
        if (null == riskConfigL || null == riskConfigL.getId()
                || StrUtil.isEmpty(riskConfigL.getModuleCode()) || null == riskConfigL.getRiskValue()) {
            throw new BizException("参数错误！");
        }
        // 根据 riskValue查询是否存在
        final LambdaQueryWrapper<RiskConfigLEntity> queryWrapper = Wrappers.<RiskConfigLEntity>lambdaQuery()
                .eq(RiskConfigLEntity::getModuleCode, riskConfigL.getModuleCode()).eq(RiskConfigLEntity::getRiskValue, riskConfigL.getRiskValue()).ne(RiskConfigLEntity::getId, riskConfigL.getId());
        final List<RiskConfigLEntity> riskConfigLEntityList = baseMapper.selectList(queryWrapper);
        if (CollUtil.isNotEmpty(riskConfigLEntityList)) {
            throw new BizException("值/等级已存在！");
        }
        return baseMapper.updateById(riskConfigL) > 0;
    }


    @Override
    public ValidateImportBO<RiskConfigLExcelDTO> validateImport(List<RiskConfigLExcelDTO> excelDTOList) {
        ValidateImportBO<RiskConfigLExcelDTO> bo = new ValidateImportBO<>();
        List<ErrorMessage> errorMessageList = new ArrayList<>();
        List<RiskConfigLExcelDTO> saveList = new ArrayList<>(excelDTOList);
        int successNum = excelDTOList.size();
        int failedNum = 0;

        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);
        return bo;
    }

    @Override
    public RiskConfigLEntity importDtoToEntity(RiskConfigLExcelDTO excelDto) {
        RiskConfigLEntity entity= new RiskConfigLEntity();
        BeanUtils.copyProperties(excelDto, entity);
        return entity;
    }

    @Override
    public RiskConfigLExcelVO exportEntityToVo(RiskConfigLEntity entity) {
        RiskConfigLExcelVO excelVO = new RiskConfigLExcelVO();
        BeanUtils.copyProperties(entity, excelVO);
        return excelVO;
    }
}
